<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>在Objective-C中使用协程 | KnightSama‘s Blog</title>
    <meta name="description" content="Hello, my friend!">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
  <script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js"></script>
    
    <link rel="preload" href="/assets/css/0.styles.c4ea52a5.css" as="style"><link rel="preload" href="/assets/js/app.a2483de7.js" as="script"><link rel="preload" href="/assets/js/4.0078dadf.js" as="script"><link rel="preload" href="/assets/js/1.c3e7d2b0.js" as="script"><link rel="preload" href="/assets/js/20.44781dae.js" as="script"><link rel="prefetch" href="/assets/js/10.265e4f40.js"><link rel="prefetch" href="/assets/js/11.a3a2f8c1.js"><link rel="prefetch" href="/assets/js/12.1910f8cc.js"><link rel="prefetch" href="/assets/js/13.229fa478.js"><link rel="prefetch" href="/assets/js/14.c27d4a17.js"><link rel="prefetch" href="/assets/js/15.92adafd4.js"><link rel="prefetch" href="/assets/js/16.bcee7c14.js"><link rel="prefetch" href="/assets/js/17.ee1bcf41.js"><link rel="prefetch" href="/assets/js/18.fa3c3a4e.js"><link rel="prefetch" href="/assets/js/19.1a943c57.js"><link rel="prefetch" href="/assets/js/21.1fe46223.js"><link rel="prefetch" href="/assets/js/22.6f7d46c2.js"><link rel="prefetch" href="/assets/js/23.56223d02.js"><link rel="prefetch" href="/assets/js/24.e7cad43b.js"><link rel="prefetch" href="/assets/js/25.b2902552.js"><link rel="prefetch" href="/assets/js/26.70698c07.js"><link rel="prefetch" href="/assets/js/27.6ee3d263.js"><link rel="prefetch" href="/assets/js/28.ce131de6.js"><link rel="prefetch" href="/assets/js/29.90552487.js"><link rel="prefetch" href="/assets/js/30.b3291ccc.js"><link rel="prefetch" href="/assets/js/31.ea8c441e.js"><link rel="prefetch" href="/assets/js/32.3e29bbeb.js"><link rel="prefetch" href="/assets/js/33.65e78c5e.js"><link rel="prefetch" href="/assets/js/34.3ba19d7c.js"><link rel="prefetch" href="/assets/js/35.66d3fc4a.js"><link rel="prefetch" href="/assets/js/36.f5725281.js"><link rel="prefetch" href="/assets/js/37.f5b0b5ff.js"><link rel="prefetch" href="/assets/js/38.3cae3054.js"><link rel="prefetch" href="/assets/js/39.47dc1ffe.js"><link rel="prefetch" href="/assets/js/5.f0714e2e.js"><link rel="prefetch" href="/assets/js/6.53240a5f.js"><link rel="prefetch" href="/assets/js/7.dc126a6b.js"><link rel="prefetch" href="/assets/js/8.86452400.js"><link rel="prefetch" href="/assets/js/9.16ef06de.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.734d5d92.js">
    <link rel="stylesheet" href="/assets/css/0.styles.c4ea52a5.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-0cb1c278><div data-v-0cb1c278><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-0cb1c278 data-v-0cb1c278><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-da0660ba data-v-0cb1c278 data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>KnightSama‘s Blog</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div class="hide" data-v-0cb1c278><header class="navbar" data-v-0cb1c278><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/Misaka.jpg" alt="KnightSama‘s Blog" class="logo"> <span class="site-name">KnightSama‘s Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-0cb1c278></div> <aside class="sidebar" data-v-0cb1c278><div class="personal-info-wrapper" data-v-4a3def3c><img src="/Misaka.jpg" alt="author-avatar" class="personal-img" data-v-4a3def3c> <h3 class="name" data-v-4a3def3c>
    KnightSama
  </h3> <div class="num" data-v-4a3def3c><div data-v-4a3def3c><h3 data-v-4a3def3c>27</h3> <h6 data-v-4a3def3c>文章</h6></div> <div data-v-4a3def3c><h3 data-v-4a3def3c>14</h3> <h6 data-v-4a3def3c>标签</h6></div></div> <hr data-v-4a3def3c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>在Objective-C中使用协程</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Views/20190315.html#什么是协程" class="sidebar-link">什么是协程</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20190315.html#如何在objective-c中使用协程" class="sidebar-link">如何在Objective-C中使用协程</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/Views/20190315.html#coobjc-的使用" class="sidebar-link">coobjc 的使用</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20190315.html#安装" class="sidebar-link">安装</a></li><li class="sidebar-sub-header"><a href="/Views/20190315.html#创建协程" class="sidebar-link">创建协程</a></li><li class="sidebar-sub-header"><a href="/Views/20190315.html#await" class="sidebar-link">Await</a></li><li class="sidebar-sub-header"><a href="/Views/20190315.html#取消操作" class="sidebar-link">取消操作</a></li><li class="sidebar-sub-header"><a href="/Views/20190315.html#错误处理" class="sidebar-link">错误处理</a></li><li class="sidebar-sub-header"><a href="/Views/20190315.html#在协程中暂停" class="sidebar-link">在协程中暂停</a></li><li class="sidebar-sub-header"><a href="/Views/20190315.html#生成器" class="sidebar-link">生成器</a></li><li class="sidebar-sub-header"><a href="/Views/20190315.html#actor" class="sidebar-link">Actor</a></li><li class="sidebar-sub-header"><a href="/Views/20190315.html#元组" class="sidebar-link">元组</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-da0660ba data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>在Objective-C中使用协程</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div data-v-0cb1c278><main class="page"><div class="sidebar-open-close"><svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <!----> <div class="page-title" style="display:none;"><h1>在Objective-C中使用协程</h1> <hr> <div data-v-73367bb4><i class="iconfont reco-account" data-v-73367bb4><span data-v-73367bb4>KnightSama</span></i> <i class="iconfont reco-date" data-v-73367bb4><span data-v-73367bb4>2019-03-15</span></i> <i class="iconfont reco-eye" data-v-73367bb4><span id="/Views/20190315.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-73367bb4><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-73367bb4><span class="tag-item" data-v-73367bb4>
      iOS
    </span><span class="tag-item" data-v-73367bb4>
      Objective-C
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="什么是协程"><a href="#什么是协程" class="header-anchor">#</a> 什么是协程</h2> <p>协程是一种在非抢占式多任务场景下生成可以在特定位置挂起和恢复执行入口的程序组件。协程是一个特殊的函数，它可以在某个地方挂起，并且可以重新在挂起处继续运行。
一个进程可以包含多个线程，一个线程也可以包含多个协程。也就是说，一个线程内可以有多个那样的特殊函数在运行。但是一个线程内的多个协程的运行是串行的。如果有多核 CPU 的话，多个进程或一个进程内的多个线程是可以并行运行的，但是一个线程内的多个协程却绝对串行的，当一个协程运行时，其他协程必须挂起。</p> <h2 id="如何在objective-c中使用协程"><a href="#如何在objective-c中使用协程" class="header-anchor">#</a> 如何在 Objective-C 中使用协程</h2> <p>很多语言自带对协程的支持，例如 Lua、Python 等，但是 Objective-C 并没有自带这种支持，因此在 iOS 的开发过程中很难使用协程，直到阿里开源了<a href="https://github.com/alibaba/coobjc" target="_blank" rel="noopener noreferrer"> coobjc<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。
coobjc 是阿里的开源库，它为 Objective-C 与 Swift 提供了协程功能。通过使用协程我们可以将很多异步逻辑通过同步的方式来编写，大大提高了可读性。此外与多线程相比，协程不需要内核级的线程切换，大大提高了性能，同时可以减少锁的使用，提高应用的整体性能。</p> <h2 id="coobjc-的使用"><a href="#coobjc-的使用" class="header-anchor">#</a> coobjc 的使用</h2> <h3 id="安装"><a href="#安装" class="header-anchor">#</a> 安装</h3> <p>使用 cocopods 引入主要的文件，Objective-C 使用 coobjc,Swift 使用 coswift,cokit 内提供了对 Foundation 和 UIKit 框架内系统原生类的协程支持</p> <div class="language-ruby line-numbers-mode"><pre class="language-ruby"><code>pod <span class="token string">&quot;coobjc&quot;</span>
pod <span class="token string">&quot;cokit&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="创建协程"><a href="#创建协程" class="header-anchor">#</a> 创建协程</h3> <p>我们可以使用下面两个方法创建协程</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 在当前队列创建协程</span>
<span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// do something</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 在指定的队列q创建协程</span>
<span class="token function">co_launch_onqueue</span><span class="token punctuation">(</span>q<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// do something</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>使用示例</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;aaaa&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">co_launch_onqueue</span><span class="token punctuation">(</span><span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;bbbb&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;cccc&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>cccc
bbbb
aaaa
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h3 id="await"><a href="#await" class="header-anchor">#</a> Await</h3> <p>我们可以使用 Promises 或者 Channel 创建异步方法并通过 <code>await</code>  在协程中使用。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 使用Promises</span>
<span class="token comment">// 通过COPromise来创建异步事件</span>
<span class="token operator">-</span> <span class="token punctuation">(</span>COPromise <span class="token operator">*</span><span class="token punctuation">)</span>doSomethingAsync<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">[</span>COPromise promise<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">(</span>COPromiseFullfill  _Nonnull fullfill<span class="token punctuation">,</span> COPromiseReject  _Nonnull reject<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 模拟异步的事件</span>
        <span class="token function">dispatch_async</span><span class="token punctuation">(</span><span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
            <span class="token comment">// do something</span>
            <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;异步操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 定义错误</span>
            NSError <span class="token operator">*</span>error <span class="token operator">=</span> nil<span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 错误的回调</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token function">fullfill</span><span class="token punctuation">(</span><span class="token string">@&quot;result&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 正确结果的回调</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------start------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    id ret <span class="token operator">=</span> <span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token keyword">self</span> doSomethingAsync<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取可能存在的错误</span>
    NSError <span class="token operator">*</span>error <span class="token operator">=</span> <span class="token function">co_getError</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;%@&quot;</span><span class="token punctuation">,</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
        <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;output:%@&quot;</span><span class="token punctuation">,</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------end------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>------start------
异步操作
output:result
------end------
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 使用Channel</span>
<span class="token comment">// 通过Channel来创建事件</span>
<span class="token operator">-</span> <span class="token punctuation">(</span>COChan <span class="token operator">*</span><span class="token punctuation">)</span>doSomethingAsync<span class="token punctuation">{</span>
    COChan <span class="token operator">*</span>chan <span class="token operator">=</span> <span class="token punctuation">[</span>COChan chan<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
        <span class="token comment">// do something</span>
        <span class="token comment">// 发送正确的结果</span>
        <span class="token comment">// send方法需要在协程中使用</span>
        <span class="token punctuation">[</span>chan send<span class="token punctuation">:</span><span class="token string">@&quot;result&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> chan<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------start------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    id ret <span class="token operator">=</span> <span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token keyword">self</span> doSomethingAsync<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取结果</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;output:%@&quot;</span><span class="token punctuation">,</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------end------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>------start------
output:result
------end------
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h3 id="取消操作"><a href="#取消操作" class="header-anchor">#</a> 取消操作</h3> <p>协程可以通过 <code>cancel</code>  方法取消</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>COCoroutine <span class="token operator">*</span>coroutine <span class="token operator">=</span> <span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------start------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">co_delay</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// sleep</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">co_isCancelled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// Do some cleaning operations</span>
        <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------cancel------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------end------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">[</span>coroutine cancel<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>------start------   
------cancel------   
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></section></div></div> <h3 id="错误处理"><a href="#错误处理" class="header-anchor">#</a> 错误处理</h3> <p>协程中通过 <code>co_getError</code>  来获取错误信息</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 通过COPromise来创建异步事件</span>
<span class="token operator">-</span> <span class="token punctuation">(</span>COPromise <span class="token operator">*</span><span class="token punctuation">)</span>doSomethingAsync<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">[</span>COPromise promise<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">(</span>COPromiseFullfill  _Nonnull fullfill<span class="token punctuation">,</span> COPromiseReject  _Nonnull reject<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">dispatch_async</span><span class="token punctuation">(</span><span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
            <span class="token comment">// do something</span>
            <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;异步操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 定义错误</span>
            NSError <span class="token operator">*</span>error <span class="token operator">=</span> <span class="token punctuation">[</span>NSError errorWithDomain<span class="token punctuation">:</span><span class="token string">@&quot;自定义错误&quot;</span> code<span class="token punctuation">:</span><span class="token number">999999</span> userInfo<span class="token punctuation">:</span>nil<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">dispatch_async</span><span class="token punctuation">(</span><span class="token function">dispatch_get_main_queue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
                    <span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 错误的回调</span>
                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token function">fullfill</span><span class="token punctuation">(</span><span class="token string">@&quot;result&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 正确结果的回调</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------start------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    id ret <span class="token operator">=</span> <span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token keyword">self</span> doSomethingAsync<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取可能存在的错误</span>
    NSError <span class="token operator">*</span>error <span class="token operator">=</span> <span class="token function">co_getError</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;%@&quot;</span><span class="token punctuation">,</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
        <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;output:%@&quot;</span><span class="token punctuation">,</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------end------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>------start------
异步操作
Error Domain=自定义错误 Code=999999 &quot;(null)&quot;
------end------
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <div class="custom-block danger"><p>测试发现 <code>reject</code>  必须与调用的协程在相同的线程队列中运行错误才能正确的获取，否则会抛出一个异常</p></div> <h3 id="在协程中暂停"><a href="#在协程中暂停" class="header-anchor">#</a> 在协程中暂停</h3> <p>在协程中可以使用 <code>co_delay</code>  来暂停当前协程，暂停当前的协程不会影响其他协程的运行</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 中间有暂停的协程</span>
<span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------start------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">co_delay</span><span class="token punctuation">(</span><span class="token number">2.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 当前协程暂停2s</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;------end------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 另一个协程</span>
<span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;前一个协程暂停，这个协程可以运行&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>------start------
前一个协程暂停，这个协程可以运行
------end------
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h3 id="生成器"><a href="#生成器" class="header-anchor">#</a> 生成器</h3> <div class="custom-block tip"><p>生成器 (Generator) 是一次生成一个值的特殊类型函数。可以将其视为可恢复函数。简单的说就是在函数的执行过程中，yield 语句会把你需要的值返回给调用生成器的地方，然后退出函数，下一次调用生成器函数的时候又从上次中断的地方开始执行，而生成器内的所有变量参数都会被保存下来供下一次使用。</p></div> <p>我们可以通过 <code>co_sequence</code>  来创建生成器，并在协程中使用它。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 创建生成器</span>
COCoroutine <span class="token operator">*</span>generator <span class="token operator">=</span> <span class="token function">co_sequence</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token function">co_isActive</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">yield_val</span><span class="token punctuation">(</span><span class="token operator">@</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 调用生成器将index的值返回</span>
        index<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token comment">// 每次调用生成器后将index的值加1</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">co_launch</span><span class="token punctuation">(</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 循环调用生成器5次</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>NSInteger index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> index <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;生成器返回:%@&quot;</span><span class="token punctuation">,</span><span class="token punctuation">[</span>generator next<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>生成器返回:0
生成器返回:1
生成器返回:2
生成器返回:3
生成器返回:4
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section></div></div> <h3 id="actor"><a href="#actor" class="header-anchor">#</a> Actor</h3> <p>Actor 是一种并发模型，Actor 模型内部的状态由它自己维护，即它内部数据只能由它自己修改 (通过消息传递来进行状态修改)，所以使用 Actors 模型进行并发编程可以很好地避免加锁等线程问题，Actor 由状态 (state)、行为 (Behavior) 和邮箱 (mailBox) 三部分组成。</p> <div class="custom-block tip"><p>状态 (state)：Actor 中的状态指的是 Actor 对象的变量信息，状态由 Actor 自己管理，避免了并发环境下的锁和内存原子性等问题</p> <p>行为 (Behavior)：行为指定的是 Actor 中计算逻辑，通过 Actor 接收到消息来改变 Actor 的状态</p> <p>邮箱 (mailBox)：邮箱是 Actor 和 Actor 之间的通信桥梁，邮箱内部通过 FIFO 消息队列来存储发送方 Actor 消息，接受方 Actor 从邮箱队列中获取消息</p></div> <p>在 coobjc 中通过 <code>co_actor_onqueue</code>  在指定队列中创建 Actor, 通过 <code>sendMessage</code>  向 Actor 发送消息</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="objective-c" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 创建一个Actor</span>
COActor <span class="token operator">*</span>actor <span class="token operator">=</span> <span class="token function">co_actor_onqueue</span><span class="token punctuation">(</span><span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">(</span>COActorChan <span class="token operator">*</span> _Nonnull event<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>COActorMessage <span class="token operator">*</span>message <span class="token keyword">in</span> event<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 打印Actor接受到的消息</span>
        <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;%@&quot;</span><span class="token punctuation">,</span>message<span class="token punctuation">.</span>type<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 向actor发送消息</span>
<span class="token punctuation">[</span>actor sendMessage<span class="token punctuation">:</span><span class="token string">@&quot;message1&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>message1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h3 id="元组"><a href="#元组" class="header-anchor">#</a> 元组</h3> <p>元组是一种数据类型，他是一组数据的集合，这些数据可以是不同的数据类型。
通过 <code>co_tuple</code>  创建元组</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>COTuple <span class="token operator">*</span>tuple <span class="token operator">=</span> <span class="token function">co_tuple</span><span class="token punctuation">(</span>nil<span class="token punctuation">,</span><span class="token string">@&quot;1&quot;</span><span class="token punctuation">,</span><span class="token operator">@</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">@</span><span class="token punctuation">[</span><span class="token operator">@</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token operator">@</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token operator">@</span><span class="token punctuation">{</span><span class="token string">@&quot;key&quot;</span><span class="token punctuation">:</span><span class="token string">@&quot;value&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>可以像数组那样获取其中的数据</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>NSArray <span class="token operator">*</span>tmpArr <span class="token operator">=</span> tuple<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>可以通过 <code>co_unpack</code>  来解包元组</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>NSString <span class="token operator">*</span>stringValue<span class="token punctuation">;</span>
NSNumber <span class="token operator">*</span>numberValue<span class="token punctuation">;</span>
NSArray <span class="token operator">*</span>arrayValue<span class="token punctuation">;</span>
<span class="token function">co_unpack</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>stringValue<span class="token punctuation">,</span><span class="token operator">&amp;</span>numberValue<span class="token punctuation">,</span><span class="token operator">&amp;</span>arrayValue<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">co_tuple</span><span class="token punctuation">(</span><span class="token string">@&quot;1&quot;</span><span class="token punctuation">,</span><span class="token operator">@</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">@</span><span class="token punctuation">[</span><span class="token operator">@</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token operator">@</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-0cb1c278><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div><canvas id="vuepress-canvas-ribbon"></canvas><div class="my-popup" style="display:none;" data-v-3414bdeb><div class="my-popup-container" data-v-3414bdeb><div class="my-popup-exit" data-v-3414bdeb></div> <img src="" alt data-v-3414bdeb></div></div><div data-v-2ebabc0c></div><div class="kanbanniang" data-v-3b0f9b07><div class="banniang-container" style="display:;" data-v-3b0f9b07><div class="messageBox" style="right:68px;bottom:230px;display:none;" data-v-3b0f9b07>
      欢迎来到 KnightSama‘s Blog
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-3b0f9b07><i class="kbnfont kbn-ban-home ban-home" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-message message" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-close close" data-v-3b0f9b07></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-3b0f9b07><i class="kbnfont kbn-ban-info info" data-v-3b0f9b07></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-3b0f9b07></i></div> <canvas id="banniang" width="200" height="260" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-3b0f9b07></canvas></div> <div class="showBanNiang" style="display:none;" data-v-3b0f9b07>
    看板娘
  </div></div></div></div>
    <script src="/assets/js/app.a2483de7.js" defer></script><script src="/assets/js/4.0078dadf.js" defer></script><script src="/assets/js/1.c3e7d2b0.js" defer></script><script src="/assets/js/20.44781dae.js" defer></script>
  </body>
</html>
